package com.xms.plugin.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xms.dependency.XmsDependencyException;
import com.xms.plugin.entity.EntityPlugin;
import com.xms.schema.event.EntityMetaDeletedEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.sql.SQLException;

/**
 * <p>
 * 删除实体后，级联删除插件
 * </p>
 *
 * @author migo
 * @since 2021/11/10
 */
@Service
public class EntityPluginCascadeDeleter {
    private IEntityPluginDeleterService _entityPluginDeleterService;

    @Autowired
    public EntityPluginCascadeDeleter(IEntityPluginDeleterService entityPluginDeleterService) {
        _entityPluginDeleterService = entityPluginDeleterService;
    }

    @Async
    @EventListener
    public void onApplicationEvent(EntityMetaDeletedEvent event) throws SQLException, XmsDependencyException {
        _entityPluginDeleterService.deleteMany(new LambdaQueryWrapper<EntityPlugin>().eq(EntityPlugin::getEntityid, event.source.getEntityid()));
    }
}
